package com.hmall.order.utils;

import cn.hutool.core.util.StrUtil;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 工具类
 */
public class RequestHolder {

    /**
     * 获取用户id
     *
     * @return
     */
    public static String getUserId() {
        ServletRequestAttributes request = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        String userId = request.getRequest().getHeader("Authorization");
        //如果获取不到，直接抛异常
        if (StrUtil.isBlank(userId)) {
            throw new RuntimeException("用户ID获取失败");
        }
        return userId;
    }

}